package com.ht.utils.config;

import com.ht.utils.utils.ChineseNumToArabicNumUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
@Slf4j
public final class WordNumberConfig {
    private static final HashMap<String,String> FORMAT_NUMBER;
    static{
        FORMAT_NUMBER = new HashMap<>();
        FORMAT_NUMBER.put("decimal-%1)","0");
        FORMAT_NUMBER.put("lowerLetter-%1.","1");
        FORMAT_NUMBER.put("chineseCounting-（%1）","3");
        FORMAT_NUMBER.put("decimal-%1.","2");
        FORMAT_NUMBER.put("decimal-(%1)","6");
        FORMAT_NUMBER.put("upperLetter-%1.","7");
        FORMAT_NUMBER.put("chineseCounting-%1、","8");
        FORMAT_NUMBER.put("bullet-\uF06C","4");
        FORMAT_NUMBER.put("bullet-\uF06E","5");
    }

    public static HashMap<String,String> getCurrentFormatNumber(String xml){
        HashMap<String,String> result = null;
        if(xml != null && !xml.isEmpty()){
            result = new HashMap<>();
            SAXReader saxReader  = new SAXReader();
            Document document = null;
            try {
                document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
            } catch (DocumentException e) {
               log.error(e.getMessage(),e);
            }
            assert document != null;
            Element rootElement = document.getRootElement();
            HashMap<String,String> hashMap = new HashMap<>();
            List<Element> nums = rootElement.elements("num");
            for(Element num:nums){
                hashMap.put(num.element("abstractNumId").attributeValue("val"),num.attributeValue("numId"));
            }
            List<Element> abstractNum = rootElement.elements("abstractNum");
            for(Element one:abstractNum){
                Element lvl = one.element("lvl");
                if(FORMAT_NUMBER.get(lvl.element("numFmt").attributeValue("val")+"-"+lvl.element("lvlText").attributeValue("val"))!=null){
                    result.put(hashMap.get(one.attributeValue("abstractNumId")),FORMAT_NUMBER.get(lvl.element("numFmt").attributeValue("val")+"-"+lvl.element("lvlText").attributeValue("val")));
                }
            }
        }
        return result;
    }

    public static String getNumberOfHtml(String numId,Integer num){
        String[] params = numId.split("-");
        return switch (Integer.parseInt(params[0])) {
            case 2 -> num + ".";
            case 0 -> num + ")";
            case 3 -> "（" + ChineseNumToArabicNumUtil.arabicNumToChineseNum(num) + "）";
            case 1 -> numberToLower(num) + ".";
            case 6 -> "(" + num + ")";
            case 7 -> numberToLetter(num) + ".";
            case 8 -> ChineseNumToArabicNumUtil.arabicNumToChineseNum(num) + "、";
            case 4 -> "disc";
            case 5 -> "square";
            default -> "";
        };
    }

    /**
     * 数字转小写字母
     * 由1-26对应转换为a-z
     * @param num 需要转换的数字
     * @return 转换后的字母
     */
    private static String numberToLower(int num) {
        if (num <= 0) {
            return null;
        }
        StringBuilder letter = new StringBuilder();
        num--;
        do {
            if (!letter.isEmpty()) {
                num--;
            }
            letter.insert(0, ((char) (num % 26 + (int) 'a')));
            num = ((num - num % 26) / 26);
        } while (num > 0);
        return letter.toString();
    }

    /**
     * 数字转大写字母
     * 由1-26对应转换为A-Z
     * @param num 需要转换的数字
     * @return 转换后的字母
     */
    private static String numberToLetter(int num) {
        if (num <= 0) {
            return null;
        }
        StringBuilder letter = new StringBuilder();
        num--;
        do {
            if (!letter.isEmpty()) {
                num--;
            }
            letter.insert(0, ((char) (num % 26 + (int) 'A')));
            num = ((num - num % 26) / 26);
        } while (num > 0);
        return letter.toString();
    }

}
